//
//  LocationGotcha.m
//  We365
//
//  Created by tms on 1/5/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "LocationGotcha.h"


@implementation LocationGotcha
@synthesize locationManager, delegate;
bool didUpdate = NO;

- (void) startUpdates
{
    NSLog(@"Starting Location Updates");
    
    if (locationManager == nil)
        locationManager = [[CLLocationManager alloc] init];
    
    locationManager.delegate = self;
    
    didUpdate = NO;
    
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    [locationManager startUpdatingLocation];
}

- (void) stopUpdates
{
    didUpdate = YES;
    [locationManager stopUpdatingLocation];
}

- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your location could not be determined" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    
    [alert show];
    [alert release];
}

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (didUpdate)
        return;
    
    didUpdate = YES;
    
    [locationManager stopUpdatingLocation];
    
    [delegate newPhysicalLocation:newLocation];
}

- (void) dealloc
{
    [locationManager release];
    [super dealloc];
}

@end
